#include "mlib.h"
#include <cstdlib>

int s_to_i(char *s, int def)
{
	bool neg = false;
	if (*s == '-') {
		neg = true;
		++ s;
	}
	int x = 0;
	for (; *s != '\0'; ++s)
		if (*s <= '9' && *s >= '0') {
			x = x * 10 + (*s - '0');
		} else
			return def;
	if (neg) x = -x;
	return x;
}

int random_int() {
	int k = 1 << 15;
	int x = rand() & (k-1);
	x = (x << 15) + (rand() & (k-1));
	return x;
}

double random_real() { // return real number in [0,1]
	return (double)random_int() / (double)(rand_maxval - 1);
}


double sqr(double x) {
	return x * x;
}